package org.osflash.signals.natives;

import asunit.asserts.*;
import flash.events.Event;
import flash.events.MouseEvent;
import org.osflash.signals.support.SpriteWithNativeSignals;
import flash.display.Sprite;

class MXMLNativeSignalTest {

	@:meta(Inject())
	public var context : Sprite;
	var mxmlSprite : SpriteWithNativeSignals;
	@:meta(Before())
	public function setUp() : Void {
		mxmlSprite = new SpriteWithNativeSignals();
	}

	@:meta(After())
	public function tearDown() : Void {
		mxmlSprite = null;
	}

	@:meta(Test())
	public function mxml_object_has_NativeSignals_after_creation() : Void {
		assertTrue(Std.is(mxmlSprite.clicked, NativeSignal));
		assertTrue(Std.is(mxmlSprite.doubleClicked, NativeSignal));
		assertTrue(Std.is(mxmlSprite.addedToStage, NativeSignal));
	}

	@:meta(Test())
	public function has_eventClass_from_mxml_default_property() : Void {
		assertEquals(MouseEvent, mxmlSprite.clicked.eventClass);
	}

	@:meta(Test())
	public function has_eventType_from_mxml_attribute() : Void {
		assertEquals(MouseEvent.CLICK, mxmlSprite.clicked.eventType);
	}

	@:meta(Test())
	public function has_target_from_mxml_attribute() : Void {
		assertEquals(mxmlSprite, mxmlSprite.clicked.target);
	}

	@:meta(Test())
	public function has_eventClass_from_mxml_attribute() : Void {
		assertEquals(MouseEvent, mxmlSprite.doubleClicked.eventClass);
	}

	@:meta(Test())
	public function omitted_eventClass_defaults_to_Event() : Void {
		assertEquals(Event, mxmlSprite.addedToStage.eventClass);
	}

	@:meta(Test())
	public function add_listener_then_dispatch_calls_listener() : Void {
		var called : Bool = false;
		var handler : Dynamic = function(event : Event) : Void {
			called = true;
		}
;
		mxmlSprite.addedToStage.addOnce(handler);
		// when
		context.addChild(mxmlSprite);
		// then
		assertTrue(called);
		context.removeChild(mxmlSprite);
	}

}

